package com.fjwt.gz.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.components.oss.model.MockMultipartFile;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.components.oss.service.OssFileService;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.utils.RegKit;
import com.fjwt.gz.db.dto.AgentInfoDTO;
import com.fjwt.gz.db.dto.AgentLevelDTO;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.entity.AgentEmployeeEntity;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.OssRecordEntity;
import com.fjwt.gz.db.entity.SysUser;
import com.fjwt.gz.db.vo.AgentDestroyVO;
import com.fjwt.gz.db.vo.AgentInfoExportVO;
import com.fjwt.gz.db.vo.AgentInfoVO;
import com.fjwt.gz.service.mapper.AgentInfoMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 代理商信息表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class AgentInfoService extends ServiceImpl<AgentInfoMapper, AgentInfoEntity> {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserAuthService sysUserAuthService;
    @Autowired
    private AgentEmployeeService agentEmployeeService;
    @Autowired
    private AgentInfoMapper agentInfoMapper;
    @Autowired
    private OssFileService ossFileService;

    @Autowired
    private OssRecordService ossRecordService;


    /**
     * 分页列表
     *
     * @param iPage
     * @param agentInfoDTO
     * @return
     */
    public IPage<AgentInfoVO> page(IPage iPage, AgentInfoDTO agentInfoDTO,  String currentAgentNo) {
        //查出当前登录用户的所属机构和所有下级机构
        if (!"0".equals(currentAgentNo)){
            agentInfoDTO.setAgentNoList(this.queryAllSubAgentNo(currentAgentNo));
        }

        IPage<AgentInfoVO> pages = baseMapper.page(iPage, agentInfoDTO);

        // 查询路径层级
        Map<String, String> agentNameMap = new HashMap<>();
        if (ObjectUtils.isNotEmpty(pages) && ObjectUtils.isNotEmpty(pages.getRecords())) {
            Set<String> agentNos = pages.getRecords().stream().flatMap(item ->
                            Stream.of(item.getAgentNo1(), item.getAgentNo2(), item.getAgentNo3(), item.getAgentNo4()))
                    .filter(Objects::nonNull).collect(Collectors.toSet());
            if (ObjectUtils.isNotEmpty(agentNos)) {
                List<AgentInfoEntity> agentInfoList = baseMapper.selectBatchIds(agentNos);
                if (ObjectUtils.isNotEmpty(agentInfoList)) {
                    agentNameMap = agentInfoList.stream().collect(Collectors.toMap(AgentInfoEntity::getAgentNo, AgentInfoEntity::getAgentName));
                }
            }
        }

        for (AgentInfoVO record : pages.getRecords()) {
            record.setHierarchyPath(getHierarchyPath(record, agentNameMap));
            record.setIsSuperAdminAccount(isSuperAdminAccount(record.getLinkName(), record.getLinkPhone(), record.getAgentNo()));
            record.setLinkPhone(DesensitizedUtil.mobilePhone(record.getLinkPhone()));
            record.setLinkName(DesensitizedUtil.chineseName(record.getLinkName()));
            record.setBindAcounts(getAgentBindAccounts(record.getAgentNo()));
        }
        return pages;
    }

    private Integer isSuperAdminAccount(String linkName, String linkPhone, String agentNo) {
        if (StringUtils.isEmpty(linkName) && StringUtils.isEmpty(linkPhone)){
            return 0;
        }
        //查询该机构下是否存在该超级管理员账号
        LambdaQueryWrapper<SysUser> gw = SysUser.gw()
                .eq(SysUser::getSysType,Constants.SYS_ROLE_TYPE.PROXY)
                .eq(SysUser::getBelongInfoId, agentNo)
                .eq(SysUser::getRealname, linkName)
                .eq(SysUser::getTelphone, linkPhone);
        SysUser sysUser = sysUserService.getOne(gw);
        if (null == sysUser){
            return 0;
        }
        return 1;
    }

    /**获取绑定的账户数：只是针对每个机构号的数量**/
    public Long getAgentBindAccounts(String agentNo){
        LambdaQueryWrapper<AgentEmployeeEntity> gw = AgentEmployeeEntity.gw();
        gw.eq(AgentEmployeeEntity::getAgentNo, agentNo);
        return agentEmployeeService.count(gw);
    }


    /**
     * 获取机构层级路径
     *
     * @param agentInfoVO
     * @return
     */
    private String getHierarchyPath(AgentInfoVO agentInfoVO, Map<String, String> agentNameMap) {
        StringBuilder hierarchyPath = new StringBuilder();
        if (StringUtils.isEmpty(agentInfoVO.getPid())) {
            return agentInfoVO.getAgentShortName();
        } else {
            if (agentNameMap.containsKey(agentInfoVO.getAgentNo1())) {
                if (!hierarchyPath.isEmpty()) {
                    hierarchyPath.append("-");
                }
                hierarchyPath.append(agentNameMap.get(agentInfoVO.getAgentNo1()));
            }
            if (agentNameMap.containsKey(agentInfoVO.getAgentNo2())) {
                if (!hierarchyPath.isEmpty()) {
                    hierarchyPath.append("-");
                }
                hierarchyPath.append(agentNameMap.get(agentInfoVO.getAgentNo2()));
            }
            if (agentNameMap.containsKey(agentInfoVO.getAgentNo3())) {
                if (!hierarchyPath.isEmpty()) {
                    hierarchyPath.append("-");
                }
                hierarchyPath.append(agentNameMap.get(agentInfoVO.getAgentNo3()));
            }
            if (agentNameMap.containsKey(agentInfoVO.getAgentNo4())) {
                if (!hierarchyPath.isEmpty()) {
                    hierarchyPath.append("-");
                }
                hierarchyPath.append(agentNameMap.get(agentInfoVO.getAgentNo4()));
            }
        }

        return hierarchyPath.toString();
    }


    /**
     * 查询单个机构信息
     *
     * @param agentNo 机构号
     * @return
     */
    public AgentInfoEntity getAgentInfo(String agentNo) {
        // 查缓存
        String cacheKeyAgentInfo = CS.getCacheKeyAgentInfo(agentNo);
        AgentInfoEntity agentInfo = RedisUtil.getObject(cacheKeyAgentInfo, AgentInfoEntity.class);
        // 缓存为空查数据库
        if (ObjectUtils.isEmpty(agentInfo)) {
            AgentInfoEntity agentInfoDto = agentInfoMapper.getAgentInfoDetail(agentNo);
            // 数据库存在放入缓存
            if (ObjectUtils.isNotEmpty(agentInfoDto)) {
                RedisUtil.set(cacheKeyAgentInfo, agentInfoDto);
            }else{
                throw new BizException("所属机构有误，请核实！");
            }
            AgentInfoEntity dto = new AgentInfoEntity();
            BeanUtils.copyProperties(agentInfoDto,dto);
            return dto;
        }
        return agentInfo;
    }

    /**
     * 查询多个机构信息
     *
     * @param agentNos 机构号集合
     * @return
     */
    public List<AgentInfoEntity> getAgentInfos(List<String> agentNos) {
        if (ObjectUtils.isEmpty(agentNos)) {
            throw new BizException("请输入正确的机构号");
        }
        List<AgentInfoEntity> agentInfoList = new ArrayList<>();
        for (String agentNo : agentNos) {
            if (ObjectUtils.isNotEmpty(agentNo)) {
                AgentInfoEntity agentInfo = getAgentInfo(agentNo);
                if (ObjectUtils.isNotEmpty(agentInfo)) {
                    agentInfoList.add(agentInfo);
                }
            }
        }
        return agentInfoList;
    }

    /**
     * 查询机构层级信息
     *
     * @param agentNo 机构号
     * @return
     */
    public AgentInfoVO getAgentInfos(String agentNo) {
        // 查缓存
        String cacheKeyAgentInfos = CS.getCacheKeyAgentInfos(agentNo);
        AgentInfoVO agentInfoVo = RedisUtil.getObject(cacheKeyAgentInfos, AgentInfoVO.class);
        // 缓存为空查数据库
        if (ObjectUtils.isEmpty(agentInfoVo)) {
            AgentInfoEntity agentInfo = baseMapper.selectById(agentNo);
            if (ObjectUtils.isNotEmpty(agentInfo)) {
                agentInfoVo = new AgentInfoVO();
                BeanUtils.copyProperties(agentInfo, agentInfoVo);
                List<AgentInfoVO> agentInfos = baseMapper.list(new AgentInfoDTO());
                Map<String, List<AgentInfoVO>> agentInfoGroupByPidMap = agentInfos.stream().collect(Collectors.groupingBy(AgentInfoVO::getPid));

                getAgentTree(agentInfoVo, agentInfoGroupByPidMap);

                // 数据库存在放入缓存
                if (ObjectUtils.isNotEmpty(agentInfo)) {
                    RedisUtil.set(cacheKeyAgentInfos, agentInfoVo);
                }
            }
        }
        return agentInfoVo;
    }

    /**
     * 通过pid获取下级机构列表（只查询一级）
     *
     * @param agentNo 机构号
     * @return
     */
    public List<AgentInfoVO> querySubAgentList(String agentNo) {
        AgentInfoDTO agentInfoDTO = new AgentInfoDTO();
        if (ObjectUtils.isEmpty(agentNo)) {
            agentInfoDTO.setEmptyPid(true);
            return baseMapper.list(agentInfoDTO);
        }

        // 查缓存
        String cacheKeyAgentInfos = CS.getCacheKeyAgentInfos(agentNo);
        AgentInfoVO agentInfoVo = RedisUtil.getObject(cacheKeyAgentInfos, AgentInfoVO.class);
        // 缓存为空查数据库
        if (ObjectUtils.isEmpty(agentInfoVo)) {
            AgentInfoEntity agentInfo = baseMapper.selectById(agentNo);
            if (ObjectUtils.isNotEmpty(agentInfo)) {
                agentInfoVo = new AgentInfoVO();
                BeanUtils.copyProperties(agentInfo, agentInfoVo);
                List<AgentInfoVO> agentInfos = baseMapper.list(agentInfoDTO);
                Map<String, List<AgentInfoVO>> agentInfoGroupByPidMap = agentInfos.stream().collect(Collectors.groupingBy(AgentInfoVO::getPid));

                getAgentTree(agentInfoVo, agentInfoGroupByPidMap);

                // 数据库存在放入缓存
                if (ObjectUtils.isNotEmpty(agentInfo)) {
                    RedisUtil.set(cacheKeyAgentInfos, agentInfoVo);
                }
            }
        }
        return agentInfoVo.getChildren();
    }

    /**
     * 递归获取下级机构
     *
     * @param agentInfoVO
     * @param agentInfoGroupByPidMap
     * @return
     */
    public AgentInfoVO getAgentTree(AgentInfoVO agentInfoVO, Map<String, List<AgentInfoVO>> agentInfoGroupByPidMap) {
        if (agentInfoGroupByPidMap.containsKey(agentInfoVO.getAgentNo())) {
            List<AgentInfoVO> agentInfoVOList = agentInfoGroupByPidMap.get(agentInfoVO.getAgentNo());
            agentInfoVO.setChildren(agentInfoVOList);
            for (AgentInfoVO infoVO : agentInfoVOList) {
                getAgentTree(infoVO, agentInfoGroupByPidMap);
            }
        }
        return agentInfoVO;
    }

    /**
     * 所有的上级代理商信息
     */
    public List<AgentInfoEntity> queryAllTopAgentNo(List<AgentInfoEntity> agentInfoList, String currentAgentNo) {
        AgentInfoEntity agentInfo = this.getAgentInfo(currentAgentNo);
        if (ObjectUtils.isNotEmpty(agentInfo)) {
            agentInfoList.add(agentInfo);
            if (ObjectUtils.isNotEmpty((agentInfo.getPid()))) {
                queryAllTopAgentNo(agentInfoList, agentInfo.getPid());
            }
        }
        return agentInfoList;
    }

    /**
     * 所有的下级代理商号码 （当前 和 所有的下级代理商 ）
     */
    public List<String> queryAllSubAgentNo(String currentAgentNo) {
        List<String> result = new ArrayList<>();
        AgentInfoVO agentInfoVO = this.getAgentInfos(currentAgentNo);
        result.add(currentAgentNo);
        recursionQuerySubAgentId(result, agentInfoVO);
        return result;
    }

    /**
     * 递归查询下级代理商
     */
    private void recursionQuerySubAgentId(List<String> result, AgentInfoVO agentInfoVO) {
        // 子集为空
        if (ObjectUtils.isEmpty(agentInfoVO.getChildren())) {
            return;
        }
        for (AgentInfoVO child : agentInfoVO.getChildren()) {
            // 加入当前代理商号码
            result.add(child.getAgentNo());
            recursionQuerySubAgentId(result, child);
        }
    }


    /**
     * PC机构端自动新增管理员用户
     *
     * @param agentNo
     */
    @Transactional(rollbackFor = BizException.class)
    public void autoAddUser(String agentNo) {

        AgentInfoEntity agentInfoEntity = checkFiled(agentNo);

        //每个代理商只有一个账号 一个手机号只能绑定一个机构
        long count = sysUserService.count(
                SysUser.gw()
                        .eq(SysUser::getSysType, CS.SYS_ROLE_TYPE.PROXY)
                        .eq(SysUser::getTelphone, agentInfoEntity.getLinkPhone())
                        .eq(SysUser::getUserType, SysUser.UEST_TYPE_ADMIN)
        );
        if (count > 0) {
            throw new BizException("账号已存在！");
        }

        SysUser sysUser = new SysUser();
        sysUser.setRealname(agentInfoEntity.getLinkName());
        sysUser.setTelphone(agentInfoEntity.getLinkPhone());
        sysUser.setSex(CS.SEX_UNKNOWN);
        sysUser.setAvatarUrl("https://jdyd-jhzf-public.oss-cn-hangzhou.aliyuncs.com/gz/img/defava_m.png");
        sysUser.setUserType(SysUser.UEST_TYPE_ADMIN);
        sysUser.setState(SysUser.STATE_START);
        sysUser.setSysType(CS.SYS_ROLE_TYPE.PROXY);
        sysUser.setBelongInfoId(agentNo);
        // 设置过期时间
        sysUserService.resetSysUserPwdTime(sysUser, null);
        // 保存数据
        boolean flag = sysUserService.save(sysUser);

        log.info("用户创建成功，返回用户主键ID：{}", sysUser.getSysUserId());
        if (flag) {
            // 系统用户认证表
            sysUserAuthService.addUserAuthDefault(sysUser.getSysUserId(), agentInfoEntity.getLinkPhone(), null, CS.SYS_ROLE_TYPE.PROXY);
            //操作员<->角色 关联表
            switch (agentInfoEntity.getLevel()){
                case 1:
                    sysUserService.saveUserRole(sysUser.getSysUserId(), Collections.singletonList("ROLE_9dc153"));
                    break;
                case 2:
                    sysUserService.saveUserRole(sysUser.getSysUserId(), Collections.singletonList("ROLE_8f6659"));
                    break;
                case 3:
                    sysUserService.saveUserRole(sysUser.getSysUserId(), Collections.singletonList("ROLE_78b088"));
                    break;
                case 4:
                    sysUserService.saveUserRole(sysUser.getSysUserId(), Collections.singletonList("ROLE_aacb20"));
                    break;
            }
        } else {
            throw new BizException("新增用户信息异常！");
        }
    }

    /**
     * 重置密码
     *
     * @param agentNo
     */
    public void pwdReset(String agentNo) {

        AgentInfoEntity agentInfoEntity = checkFiled(agentNo);

        SysUser sysUser =
                sysUserService.getBaseMapper().selectOne(
                        SysUser.gw()
                                .eq(SysUser::getSysType, CS.SYS_ROLE_TYPE.PROXY)
                                .eq(SysUser::getBelongInfoId, agentNo)
                                .eq(SysUser::getTelphone, agentInfoEntity.getLinkPhone())
                );
        log.info("商户账号信息：{}", sysUser);

        if (ObjectUtil.isNull(sysUser)) {
            throw new BizException("此商户暂无账号信息！");
        }
        // 重置密码
        log.info("进行重置密码......");
        sysUserAuthService.resetAuthInfo(sysUser.getSysUserId(), null, sysUser.getTelphone(), true, null, CS.SYS_ROLE_TYPE.PROXY);
        log.info("重置密码完成......");

        // 修改密码后清除token
        Collection<String> cacheKeyList = RedisUtil.keys(CS.getCacheKeyToken(CS.SYS_ROLE_TYPE.PROXY, sysUser.getSysUserId(), "*"));
        log.info("pc机构端重置密码缓存key的值：{}", cacheKeyList);
        if (ObjectUtil.isNull(cacheKeyList) || ObjectUtil.isEmpty(cacheKeyList)) {
            return;
        }
        for (String cacheKey : cacheKeyList) {
            RedisUtil.del(cacheKey);
        }
    }

    /**
     * 检测字段信息
     * @param agentNo 机构号
     */
    private AgentInfoEntity checkFiled(String agentNo) {
        if (null == agentNo) {
            throw new BizException("请求参数缺失！");
        }
        log.info("参数agentNo: {}", agentNo);
        LambdaQueryWrapper<AgentInfoEntity> gw = AgentInfoEntity.gw();
        gw.eq(AgentInfoEntity::getAgentNo, agentNo);
        AgentInfoEntity agentInfoEntity = this.getBaseMapper().selectOne(gw);
        log.info("代理商信息：{}", agentInfoEntity);
        if (ObjectUtil.isNull(agentInfoEntity) || ObjectUtil.isEmpty(agentInfoEntity)) {
            throw new BizException("暂无此代理商！");
        }
        if (ObjectUtil.isNull(agentInfoEntity.getLinkPhone()) || ObjectUtil.isEmpty(agentInfoEntity.getLinkPhone())) {
            throw new BizException("无联系人手机号！");
        }
        if (!RegKit.isMobile(agentInfoEntity.getLinkPhone())) {
            throw new BizException("手机号格式不正确，请输入11位有效号码！");
        }
        if (ObjectUtil.isNull(agentInfoEntity.getLinkName()) || ObjectUtil.isEmpty(agentInfoEntity.getLinkName())) {
            throw new BizException("无联系人姓名！");
        }
        return agentInfoEntity;
    }

    /**实物奖品导航至使用机构的机构列表**/
    public IPage<AgentDestroyVO> getNearAgentList(IPage<?> page, PointDTO location, String radius, List<String> agentNoList) {
        return baseMapper.getNearAgentList(page, location, radius, agentNoList);
    }

    /**可核销实物商品的机构列表**/
    public IPage<AgentDestroyVO> getAgentDestroyList(IPage<?> page, PointDTO location, Long goodsId, String radius) {
        return baseMapper.getAgentDestroyList(page, location, goodsId, radius);
    }

    /**可激活卡券的机构列表**/
    public IPage<AgentDestroyVO> getCouponAgentDestroyList(IPage<?> page, PointDTO location, Long couponId, String radius) {
        return baseMapper.getAgentDestroyList(page, location, couponId, radius);
    }

    public Integer updateInfo(AgentInfoDTO agentInfoDTO) {
        //目前只修改了地址和经纬度，后续修改字段增多，继续增加
        String addr = agentInfoDTO.getAddr();
        if (StringUtils.isEmpty(addr)) {
            throw new BizException("地址不可为空");
        }
        Double lng = agentInfoDTO.getLng();
        if (null == lng){
            throw new BizException("经度不可为空");
        }
        Double lat = agentInfoDTO.getLat();
        if (null == lat){
            throw new BizException("纬度不可为空");
        }
        String agentNo = agentInfoDTO.getAgentNo();
        if (StringUtils.isEmpty(agentNo)){
            throw new BizException("机构号不可为空");
        }
        String areaCode = agentInfoDTO.getAreaCode();
        if (StringUtils.isEmpty(areaCode)){
            throw new BizException("省市县编码不可为空");
        }
        String areaName = agentInfoDTO.getAreaName();
        if (StringUtils.isEmpty(areaName)){
            throw new BizException("省市县名称不可为空");
        }
        AgentInfoEntity agentInfoEntity = new AgentInfoEntity();
        agentInfoEntity.setAgentNo(agentNo);
        agentInfoEntity.setAddr(addr);
        agentInfoEntity.setAreaCode(areaCode);
        agentInfoEntity.setAreaName(areaName);
        PointDTO pointDTO = new PointDTO();

        BigDecimal latBigDecimal = new BigDecimal(lat.toString());
        BigDecimal lngBigDecimal = new BigDecimal(lng.toString());
        latBigDecimal.setScale(6, RoundingMode.HALF_UP);
        lngBigDecimal.setScale(6, RoundingMode.HALF_UP);
        pointDTO.setLatitude(latBigDecimal);
        pointDTO.setLongitude(lngBigDecimal);
        agentInfoEntity.setPointDTO(pointDTO);

        //刪除redis数据
        String cacheKeyAgentInfo = CS.getCacheKeyAgentInfo(agentNo);
        RedisUtil.del(cacheKeyAgentInfo);
        return agentInfoMapper.updateAgemtInfoById(agentInfoEntity);
    }

    public JSONObject export(IPage<AgentInfoVO> agentInfoVOIPage, HttpServletResponse response) {
        ArrayList<AgentInfoExportVO> agentInfoExportVOS = new ArrayList<>();
        for (AgentInfoVO agentInfoVO : agentInfoVOIPage.getRecords()) {
            AgentInfoExportVO agentInfoExportVO = new AgentInfoExportVO();
            BeanUtils.copyProperties(agentInfoVO, agentInfoExportVO);
            String location = agentInfoVO.getLng()+"," + agentInfoVO.getLat();
            agentInfoExportVO.setLocation(location);
            agentInfoExportVOS.add(agentInfoExportVO);
        }

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=exportedData.xlsx");

        String successFileName = String.format("./agentInfo/%s.xlsx","机构信息" + DateUtil.date().getTime());

        File successFile = new File(successFileName);
        BigExcelWriter writer = ExcelUtil.getBigWriter(successFile);

        writer.renameSheet("机构信息导出");
        writer.addHeaderAlias("agentNo", "机构号");
        writer.addHeaderAlias("agentName", "机构名称");
        writer.addHeaderAlias("addr", "地址");
        writer.addHeaderAlias("location", "经纬度");
        writer.addHeaderAlias("bindAcounts", "绑定账户数");
        writer.setOnlyAlias(true);
        writer.write(agentInfoExportVOS);
        writer.autoSizeColumnAll();
        // 关闭writer，释放资源
        writer.close();

        try {
            MultipartFile multipartFile = new MockMultipartFile(successFileName, successFile.length(), new FileInputStream(successFile));
            String successUrl = ossFileService.singleFileUpload(multipartFile, OssFileConfig.BIZ_TYPE.UPLOAD);
            // 添加oos上传记录
            OssRecordEntity ossRecordEntity = new OssRecordEntity();
            ossRecordEntity.setBizType("scorexq");
            ossRecordEntity.setOssUrl(successUrl);
            ossRecordEntity.setFileSize(successFile.length());
            ossRecordEntity.setUploadType(Constants.OSS_UPLOAD_TYPE.AGENT_INFO_IMPORT);
            ossRecordEntity.setCreatedAt(new Date());
            ossRecordService.save(ossRecordEntity);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("successUrl", successUrl);
            //删除文件
            FileUtil.del(successFile);
            return jsonObject;
        } catch (Exception e) {
            throw new BizException(e.getMessage());
        }
    }

    public AgentLevelDTO selectAgentLevel(String agentNo){
        return agentInfoMapper.selectAgentLevel(agentNo);
    }
}
